package com.wyc.apiPassenger.service.impl;

import com.wyc.apiPassenger.feign.ServicePassengerUserFeignClient;
import com.wyc.apiPassenger.service.UserService;
import com.wyc.internalCommon.constant.CommonStatusEnum;
import com.wyc.internalCommon.result.ApiResult;
import com.wyc.internalCommon.result.TokenResult;
import com.wyc.internalCommon.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private ServicePassengerUserFeignClient passengerUserFeignClient;

    /**
     * 根据token获取用户信息
     * @param token
     * @return
     */
    @Override
    public ApiResult getUserByToken(String token) {

        // 解析token，获取用户手机号
        TokenResult tokenResult = JWTUtils.checkToken(token);
        if (null == tokenResult){
            return ApiResult.error(CommonStatusEnum.TOKEN_ERROR.getCode(),CommonStatusEnum.TOKEN_ERROR.getMsg());
        }
        String phone = tokenResult.getPhone();

        // 根据手机号查询用户信息
        return passengerUserFeignClient.getUserByPhone(phone);
    }
}
